import settings from "@/settings";
/**
 * 配置文件读取器
 * @author `wangxin 13700043840@163.com` 2020/4/17
 */
class SettingReader {
  /**
   *
   * @param {string} xpath
   * @returns {JSON}
   */
  parse(xpath) {
    let item = settings;
    if (xpath) {
      const depth = xpath.split('.');
      depth.forEach(name => {
        item = item[name] || {};
      });
    }
    return item;
  }
  /**
   * 合并参数默认值
   * @param {string} xpath 参数路径
   * @param {*} options 参数默认值
   */
  merge(options) {
    const deepObjectMerge = (source, target) => {
      for (var key in target) {
        if (source.hasOwnProperty(key)) {
          if (Object.prototype.toString.call(source[key]) === '[object Object]') {
            deepObjectMerge(source[key], target[key]);
          }
        } else {
          source[key] = target[key]
        }
      }
      return source;
    }
    deepObjectMerge(settings, options);
  }
}
const settingReader = new SettingReader();

export default settingReader
settings.parse = settingReader.parse;
settings.merge = settingReader.merge;
